<?php

namespace addons\booking\model;

use think\Db;
use addons\booking\model\HouseForm;

class Fields extends \think\Model
{

    // 表名
    protected $name = 'booking_fields';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
        'content_list',
    ];

    protected $type = [
        'setting' => 'json',
    ];
    protected static $listFields = ['select', 'selects', 'checkbox', 'radio', 'array'];

    protected static $config = [];

    protected static function init()
    {
    }

    public function getExtendHtmlAttr($value, $data)
    {
        $result = preg_replace_callback("/\{([a-zA-Z]+)\}/", function ($matches) use ($data) {
            if (isset($data[$matches[1]])) {
                return $data[$matches[1]];
            }
        }, $data['extend']);
        return $result;
    }

    /**
     * 获取字典列表字段
     * @return array
     */
    public static function getListFields()
    {
        return self::$listFields;
    }

    public function getContentListAttr($value, $data)
    {
        return in_array($data['type'], self::$listFields) ? \app\common\model\Config::decode($data['content']) : $data['content'];
    }


    /**
     * 整理自定义项目字段数据
     * @param int $source_id
     * @return array
     */
    public static function getSourceField($source_id, $source = 'house')
    {
        $data = self::field('name,title,value,type,content,setting,defaultvalue')->where('source', $source)->order('weigh desc')->select();
        $formData = HouseForm::where('house_id', $source_id)->find();
        $newData = [];
        foreach ($data as $item) {
            if ($formData && isset($formData[$item['name']])) {
                $item->value = $formData[$item['name']];
            }
            switch ($item['type']) {
                case 'files':
                case 'images':
                    $files = explode(',', $item['value']);
                    foreach ($files as &$file) {
                        $file = cdnurl($file, true);
                    }
                    $item->value = $files;
                    break;
                case 'file':
                case 'image':
                    $item['value'] = cdnurl($item['value'], true);
                    break;
                case 'array':
                    $item['value'] = (array)json_decode($item['value'], true);
                    break;
                case 'selectpages':
                    $item['value'] = Db::table($item['setting']['table'])->where($item['setting']['primarykey'], 'IN', $item['value'])->column($item['setting']['field']);
                    break;
                case 'selectpage':
                    $item['value'] = Db::table($item['setting']['table'])->where($item['setting']['primarykey'], $item['value'])->value($item['setting']['field']);
                    break;
                case 'location':
                    $item['value'] = !empty($item['value']) ? (array)json_decode($item['value'], true) : $item['value'];
                    break;
            }
            $setting = $item['setting'];
            unset($setting['table']);
            $item['setting'] = $setting;
            $newData[] = $item;
        }

        return $newData;
    }

}
